package com.longge;

import java.util.ArrayList;

public class SortArrayByParityII {

    public int[] sortArrayByParityII(int[] nums) {

        ArrayList<Integer> odd = new ArrayList<>();
        ArrayList<Integer> even = new ArrayList<>();
        ArrayList<Integer> res = new ArrayList<>();
        for (int num : nums) {
            if (num % 2 == 0) even.add(num);
            else odd.add(num);
        }
        int i = 0;
        while (i < odd.size()) {
            res.add(even.get(i));
            res.add(odd.get(i));
            i++;
        }

        int[] ans = new int[nums.length];
        for (int j = 0; j < res.size(); j++) {
            ans[j] = res.get(j);
        }
        return ans;

    }

    public static void main(String[] args) {
        new SortArrayByParityII().sortArrayByParityII(new int[]{4,2,5,7});
    }
}
